package com.shop.loan.proxy;

import com.shop.loan.mapper.EruptUserByMybatisMapper;
import com.shop.loan.model.entity.EruptUserByMybatis;
import com.shop.loan.model.enums.OpFailType;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.EruptUserDataService;
import com.shop.loan.util.OpFailUtil;
import com.shop.loan.util.SysMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.core.util.EruptSpringUtil;
import xyz.erupt.upms.base.LoginModel;
import xyz.erupt.upms.fun.LoginProxy;
import xyz.erupt.upms.model.EruptUser;
import xyz.erupt.upms.service.EruptUserService;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Service
public class LoginMsgCodeProxy implements LoginProxy {

    @Resource
    OpFailUtil opFailUtil;

    @Resource
    private HttpServletRequest request;

    @Resource
    SysMsgUtil sysMsgUtil;

    @Resource
    EruptUserByMybatisMapper eruptUserByMybatisMapper;

    @Resource
    RedisUtil redisUtil;

    @Resource
    EruptUserDataService eruptUserDataService;

    @Override
    public EruptUser login(String account, String pwd) {
        String msgCode = request.getParameter("msgCode");
        if (!StringUtils.hasLength(msgCode)) {
            throw new RuntimeException("短信验证码不能为空");
        }
        EruptUserByMybatis eruptUserByMybatis = eruptUserByMybatisMapper.selectByAccount(account);
        if (eruptUserByMybatis == null) {
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), null, OpFailType.login);
            throw new RuntimeException("账号密码错误");
        }
        if (!eruptUserByMybatis.getStatus()) {
            throw new RuntimeException("账号状态异常");
        }
        boolean status = sysMsgUtil.checkVerifyCode(eruptUserByMybatis.getPhone(), msgCode);
        if (!status) {
            if (StringUtils.hasLength(eruptUserByMybatis.getMsgCode()) && eruptUserDataService.checkChannelAuth(eruptUserByMybatis.getId())
                    && eruptUserByMybatis.getMsgCode().trim().equals(msgCode)) {
                log.info("渠道根据固定验证码登录成功");
            } else {
                //验证码错误
                Long eruptUserId = null;
                if (eruptUserByMybatis.getId() != null) {
                    eruptUserId = eruptUserByMybatis.getId();
                }
                opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserId, OpFailType.login);
                throw new RuntimeException("无效短信验证码");
            }
        }
        LoginModel loginModel = EruptSpringUtil.getBean(EruptUserService.class).login(account, pwd);
        if (loginModel.isPass()) {
            String requestIp = IpUtil.getIpAddr(request);
            redisUtil.set(RedisCache.eruptUserLastLoginIp + loginModel.getEruptUser().getId(), requestIp);
            if (StringUtils.hasLength(loginModel.getEruptUser().getPhone())) {
                sysMsgUtil.sendLoginSuccessCodeByAdmin(loginModel.getEruptUser().getName(), loginModel.getEruptUser().getPhone());
            }
            //删除图形验证码记录
            redisUtil.del(RedisCache.userLoginPicCode + loginModel.getEruptUser().getAccount());
            return loginModel.getEruptUser();
        } else {
            //账号密码错误  记录错误次数和ip
            Long eruptUserId = null;
            if (loginModel.getEruptUser() != null) {
                eruptUserId = loginModel.getEruptUser().getId();
            }
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserId, OpFailType.login);
            throw new RuntimeException(loginModel.getReason());
        }
    }


}
